#include "scenevisualizer.h"
#include <iostream>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QGraphicsView>
#include "config.h"
#include <QScrollBar>
SceneVisualizer::SceneVisualizer()
{

}

void SceneVisualizer::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    std::cout<< "Move: " << event->pos().x() << " " << event->pos().y() <<std::endl;
}


void SceneVisualizer::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    std::cout<< "Press: " << event->pos().x() << " " << event->pos().y() <<std::endl;
}

void SceneVisualizer::wheelEvent(QGraphicsSceneWheelEvent *event)
{
 std::cout<< "weel: " <<std::endl;
}

void SceneVisualizer::drawForeground(QPainter* painter, const QRectF& rect) {

    QGraphicsView * view = this->views()[0];
    QRectF view_rect = view->rect();

    QPointF  left_bottom = /*view->mapToScene*/QPointF(view_rect.left(),view_rect.bottom());
    QPointF  right_bottom = /*view->mapToScene*/QPointF(view_rect.right(),view_rect.bottom());

    QPointF  left_top = /*view->mapToScene*/QPointF(view_rect.left(),view_rect.top());
    QPointF  right_top = /*view->mapToScene*/QPointF(view_rect.right(),view_rect.top());


    double stoke_height = Config::strokesHeight();


    //drawHorizontalAxis(left_bottom, right_bottom,stoke_height, painter);
    drawHorizontalAxis(left_top, right_top, stoke_height,painter);

    //drawVerticalAxis(left_bottom, left_top, stoke_height, painter);
    drawVerticalAxis(right_bottom, right_top ,stoke_height, painter);
}

void SceneVisualizer::drawHorizontalAxis(QPointF p1, QPointF p2, double strok_height, QPainter *painter)
{
    QTransform  t = painter->transform();
    painter->resetTransform();

    painter->setPen(QPen(Qt::black, 0));

    //draw strokes

    int scroll =  this->views()[0]->horizontalScrollBar()->value();
    int h = abs((p2.x() - p1.x()) / Config::strokesCount());

    for (int i = 0; i< Config::strokesCount(); i++)
    {
        QPointF strok_p1 = p1+QPointF((int)(i*h)+scroll%h,-strok_height);
        QPointF strok_p2 = p1+QPointF((int)(i*h)+scroll%h,strok_height);
        QPointF mx = this->views()[0]->mapToScene(strok_p1.x(),strok_p1.y());
        //t.map(p2);


        painter->drawLine(strok_p1, strok_p2);
        double val = mx.x();//p1+QPointF((int)(i*h)+scroll,-strok_height)).x()/t.m11();
        painter->drawText(strok_p2+QPointF(1,-strok_height+10),QString::number(val));

    }
    painter->setTransform(t);
}
void SceneVisualizer::drawVerticalAxis(QPointF p1, QPointF p2, double strok_height, QPainter *painter)
{
    QTransform  t = painter->transform();
    painter->resetTransform();

    painter->setPen(QPen(Qt::black, 0));

    //draw strokes

    int scroll =  this->views()[0]->verticalScrollBar()->value();
    int h = abs((p2.y() - p1.y()) / Config::strokesCount());

    for (int i = 1; i< Config::strokesCount()+1; i++)
    {
        QPointF strok_p1 = p2+QPointF(-strok_height,(int)(i*h)+scroll%h);
        QPointF strok_p2 = p2+QPointF(strok_height, (int)(i*h)+scroll%h);
        QPointF my = this->views()[0]->mapToScene(strok_p1.x(),strok_p1.y());
        painter->drawLine(strok_p1, strok_p2);
        double val = my.y();
        painter->drawText(strok_p1+QPointF(-45+strok_height,-1),QString::number(val));

    }
    painter->setTransform(t);
}
